// components/Mutual/Mutual.js
const {
  request
} = require("../../utils/index.js")
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    item: Object
  },

  /**
   * 组件的初始数据
   */
  data: {
    artId: "",
    word: true, //评论展开
    comments: [], //评论列表数据
    inputValue: "", //评论内容
  },
  ready() {
    this.setData({
      artId: this.data.item.artId
    })
    this.getpinglun()
  },
  /**
   * 组件的方法列表
   */
  methods: {


    // 点击查看图片
    handleSwiper(e) {
      var that = this;
      var urlList = [];
      var scenePics = that.data.item.files
      for (var i = 0; i < scenePics.length; i++) {
        urlList.push(scenePics[i].updUrl);
      }
      wx.previewImage({
        urls: urlList,
        current: urlList[e.currentTarget.dataset.id]
      })
    },
    //跳转至详情页
    coopera: function (potion) {
      wx.navigateTo({
        url: '../../pages/Mutualdetails/details?artId=' + this.data.artId,
      })
    },
    //互助点赞增加
    hzdianzan: async function (e) {
      let param = {
        url: "hw/portal/user/praise/save",
        header: {
          'content-type': 'application/x-www-form-urlencoded', // 默认值
        },
        data: {
          "token": wx.getStorageSync('token'),
          "praiseModule": "mutual_information",
          "objId": this.data.artId,
        },
        method: "POST"
      }
      let res = await request(param);
      if (res.status == 100) {
        this.setData({
          "item.isPraise": !this.data.item.isPraise,
          "item.praiseCount": ++this.data.item.visitsCount
        })
      }
    },
    //互助点赞取消
    hzcanceldianzan: async function () {
      let param = {
        url: "hw/portal/user/praise/del",
        header: {
          'content-type': 'application/x-www-form-urlencoded', // 默认值
        },
        data: {
          "token": wx.getStorageSync('token'),
          "praiseModule": "mutual_information",
          "objId": this.data.artId, //当前用户id
        },
        method: "POST"
      }
      let res = await request(param);
      if (res.status == 100) {
        this.setData({
          "item.isPraise": !this.data.item.isPraise,
          "item.praiseCount": --this.data.item.visitsCount
        })
      }
    },
    //互助收藏增加
    hzshoucang: async function (e) {
      let param = {
        url: "hw/portal/user/collect/save",
        header: {
          'content-type': 'application/x-www-form-urlencoded', // 默认值
        },
        data: {
          "token": wx.getStorageSync('token'),
          "collectModule": "mutual_information",
          "objId": this.data.artId,
        },
        method: "POST"
      }
      let datainfo = await request(param);
      if (datainfo.status == 100) {
        this.setData({
          "item.isCollect": !this.data.item.isCollect,
          "item.collectCount": ++this.data.item.collectCount
        })
      }
    },
    //互助收藏取消
    hzcanceshoucang: async function () {
      let parm = {
        url: "hw/portal/user/collect/del",
        header: {
          'content-type': 'application/x-www-form-urlencoded', // 默认值
        },
        data: {
          "token": wx.getStorageSync('token'),
          "collectModule": "mutual_information",
          "objId": this.data.artId, //当前用户id
        },
        method: "POST"
      }
      let datainfo = await request(parm);
      if (datainfo.status == 100) {
        this.setData({
          "item.isCollect": !this.data.item.isCollect,
          "item.collectCount": --this.data.item.collectCount
        })
      }
    },
    //评论展开或收起
    word: function () {
      this.setData({
        word: !this.data.word
      })
      console.log(this.data.word)
    },
    //评论
    async getpinglun() {
      var that = this;
      let par = {
        data: {
          cmtModule: 'mutual_information', //需要查看评论对象
          // pageNum: 1, //页码
          // pageSize: 5, //每页条数
          objId: this.data.artId, //需要查看评论对象的ID
        },
        url: 'hw/portal/comment/page?token=' + wx.getStorageSync('token'),
        method: "get"
      }
      let res = await request(par);
      if (res.status == 100) {
        this.setData({
          comments: res.data.list
        })
      }
    },
    //评论内容
    blur(e) {
      this.setData({
        inputValue: e.detail.value
      });
    },
    async enter() {
      let that = this;
      if (that.data.inputValue == "") {
        wx.showToast({
          title: '请填写评论内容',
          icon: "none",
        })
      } else {
        let param = {
          url: "hw/portal/comment/save",
          method: "POST",
          header: {
            'content-type': 'application/x-www-form-urlencoded', // 默认值
          },
          data: {
            "token": wx.getStorageSync('token'),
            cmtContent: that.data.inputValue,
            cmtModule: "mutual_information",
            objId: this.data.item.artId, //id
          }
        }
        let data = await request(param);
        let list = [];
        if (that.data.comments) {
          list = [...that.data.comments, data.data]
        } else {
          list.push(data.data)
        }
        if (data.status == 100) {
          that.setData({
            inputValue: "",
            comments: list
          })
          wx.showToast({
            title: '发送成功',
            icon: "none",
          })
        }
      }
    },
      // 视频展示
      chooseshow: function (e) {
        var that = this;
        wx.previewMedia({
          current: 0,
          type: 'video',
          url: that.data.item.files[0].updUrl
        })
      },
  }
})